<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_pendaftaran extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'd.kd_prodi';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$PMBPeriodID = isset($_POST['PMBPeriodID']) ? strval($_POST['PMBPeriodID']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id_pendaftaran, a.PMBPeriodID, a.no_pendaftaran, a.nama, a.tempat_lahir, a.tgl_lahir, a.alamat, a.asal_sekolah, a.no_telp, a.email, b.nama_fakultas as fakultas1, d.nama_prodi as prodi1, c.nama_fakultas as fakultas2, e.nama_prodi as prodi2, a.status');
			$this->db->from('pmb_pendaftaran a');
			$this->db->join('ref_fakultas b', 'b.kd_fakultas = a.fakultas1');
			$this->db->join('ref_fakultas c', 'c.kd_fakultas = a.fakultas2');
			$this->db->join('ref_prodi d', 'd.kd_prodi = a.prodi1');
			$this->db->join('ref_prodi e', 'e.kd_prodi = a.prodi2');
			
			if($PMBPeriodID != ''){
				$this->db->where('a.PMBPeriodID', $PMBPeriodID);
			}
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		//$this->db->select('a.id_pendaftaran, a.PMBPeriodID, a.no_pendaftaran, a.nama, a.alamat, a.asal_sekolah, a.no_telp, a.email, b.nama_fakultas as fakultas1, d.nama_prodi as prodi1, c.nama_fakultas as fakultas2, e.nama_prodi as prodi2, a.status, a.nama_ortu, a.alamat_ortu, a.pekerjaan');
		$this->db->select('a.id_pendaftaran, a.PMBPeriodID, a.no_pendaftaran, a.nama, a.alamat, a.asal_sekolah, a.no_telp, a.email, a.fakultas1, a.prodi1, a.fakultas2, a.prodi2, a.status, a.nama_ortu, a.alamat_ortu, a.pekerjaan');
		$this->db->from('pmb_pendaftaran a');
		// $this->db->join('ref_fakultas b', 'b.kd_fakultas = a.fakultas1');
		// $this->db->join('ref_fakultas c', 'c.kd_fakultas = a.fakultas2');
		// $this->db->join('ref_prodi d', 'd.kd_prodi = a.prodi1');
		// $this->db->join('ref_prodi e', 'e.kd_prodi = a.prodi2');
		$this->db->where('id_pendaftaran', $kode);
		
		return $this->db->get();
	}
	
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('id_pendaftaran',$kode);
		
		$this->db->select('*');
		$this->db->from('pmb_pendaftaran');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('PMBPeriodID', $data['PMBPeriodID']);
		$this->db->set('no_pendaftaran', $data['no_pendaftaran']);
		$this->db->set('nama', $data['nama']);
		$this->db->set('tempat_lahir', $data['tempat_lahir']);
		$this->db->set('tgl_lahir', FormatDateToMysql($data['tgl_lahir']));
		//$this->db->set('nama', $data['nama']);
		$this->db->set('kota', $data['kd_kabup']);
		$this->db->set('alamat', $data['alamat']);
		$this->db->set('asal_sekolah', $data['asal_sekolah']);
		$this->db->set('no_telp', $data['no_telp']);
		$this->db->set('email', $data['email']);
		$this->db->set('fakultas1', $data['fakultas1']);
		$this->db->set('prodi1', $data['prodi1']);
		$this->db->set('fakultas2', $data['fakultas2']);
		$this->db->set('prodi2', $data['prodi2']);
		$this->db->set('nama_ortu', $data['nama_ortu']);
		$this->db->set('alamat_ortu', $data['alamat_ortu']);
		$this->db->set('pekerjaan', $data['pekerjaan']);
		$this->db->set('status', $data['status']);
		
		$this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
		
		$result = $this->db->insert('pmb_pendaftaran');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('PMBPeriodID', $data['PMBPeriodID']);
		$this->db->set('no_pendaftaran', $data['no_pendaftaran']);
		$this->db->set('nama', $data['nama']);
		$this->db->set('tempat_lahir', $data['tempat_lahir']);
		$this->db->set('tgl_lahir', FormatDateToMysql($data['tgl_lahir']));
		$this->db->set('kota', $data['kd_kabup']);
		$this->db->set('alamat', $data['alamat']);
		$this->db->set('asal_sekolah', $data['asal_sekolah']);
		$this->db->set('no_telp', $data['no_telp']);
		$this->db->set('email', $data['email']);
		$this->db->set('fakultas1', $data['fakultas1']);
		$this->db->set('prodi1', $data['prodi1']);
		$this->db->set('fakultas2', $data['fakultas2']);
		$this->db->set('prodi2', $data['prodi2']);
		$this->db->set('nama_ortu', $data['nama_ortu']);
		$this->db->set('alamat_ortu', $data['alamat_ortu']);
		$this->db->set('pekerjaan', $data['pekerjaan']);		
		$this->db->set('status', $data['status']);		
		$this->db->where('id_pendaftaran', $data['id_pendaftaran']);
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$result = $this->db->update('pmb_pendaftaran');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($kode){
		$this->db->flush_cache();
		$this->db->where('id_pendaftaran', $kode);
		$result = $this->db->delete('pmb_pendaftaran'); 
		
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function get_pdf(){
		# get data
		$this->db->flush_cache();
		$this->db->select('a.id_pendaftaran, a.PMBPeriodID, a.no_pendaftaran, a.nama, a.alamat, a.asal_sekolah, a.no_telp, a.email, b.nama_fakultas as fakultas1, d.nama_prodi as prodi1, c.nama_fakultas as fakultas2, e.nama_prodi as prodi2, a.status');
		$this->db->from('pmb_pendaftaran a');
		$this->db->join('ref_fakultas b', 'b.kd_fakultas = a.fakultas1');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = a.fakultas2');
		$this->db->join('ref_prodi d', 'd.kd_prodi = a.prodi1');
		$this->db->join('ref_prodi e', 'e.kd_prodi = a.prodi2');

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Periode / Gelombang', 'width'=>20, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'No Pendaftaran', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama', 'width'=>35, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Alamat', 'width'=>35, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Asal Sekolah', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'No Telepon', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Email', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Pilihan 1', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Pilihan 2', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				$row->PMBPeriodID,
				$row->no_pendaftaran,
				$row->nama,
				$row->alamat,
				$row->asal_sekolah,
				$row->no_telp,
				$row->email,
				$row->prodi1,
				$row->prodi2
			));
			$i++;
		}
		
		return $pdfdata;
		
	}
	
	function get_kartu($kode){
		$this->db->flush_cache();
		$this->db->start_cache();
		
		$this->db->select('a.id_pendaftaran, a.PMBPeriodID, a.no_pendaftaran, a.nama, a.alamat, a.asal_sekolah, b.nama_prodi as prodi1, c.nama_fakultas as fakultas1, d.nama_prodi as prodi2, e.nama_fakultas as fakultas2');
		$this->db->from('pmb_pendaftaran a');
		
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.prodi1');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = a.fakultas1');
		$this->db->join('ref_prodi d', 'd.kd_prodi = a.prodi2');
		$this->db->join('ref_fakultas e', 'e.kd_fakultas = a.fakultas2');
		
		$this->db->where('id_pendaftaran', $kode);
		
		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
			foreach ($result->result() as $data) {
				$data_pdf[] = $data;
			}
			return $data_pdf;           
		}
	}
}

?>